#ifndef __CACHEALLOC_H__
#define __CACHEALLOC_H__

#include "cacheThread.h"
#include "cachePage.h"


static void* cAlloc(size_t size)
{
    if(size > sizeClass::kMaxSize)
    {
        size_t size_class = sizeClass::getRoundUp(size);

        size_t pagenum = size_class >> PAGE_SHIFT;
        qSpan* sp = PageCache::getInstance()->getSpan(pagenum);
        if(!sp)
            return nullptr;
        
        sp->size_ = size_class;
        return reinterpret_cast<void*>(sp->pageid_ << PAGE_SHIFT);
    }
    else
    {
        threadCache* tc_data = threadCache::getThreadCache();
        if(tc_data)
        {
            tc_data->allocate(size);
        }
    }
}

static void cFree(void* ptr)
{
    //size_t pageid = reinterpret_cast<uintptr_t>(ptr) >> PAGE_SHIFT;
    qSpan* sp = PageCache::getInstance()->getSpanByPageid(ptr);
    if(sp)
    {
        if(sp->size_ > sizeClass::kMaxSize)
        {
            PageCache::getInstance()->systemFreePage(ptr, sp->size_);
        }
        else
        {
            threadCache* tc_data = threadCache::getThreadCache();
            if(tc_data)
            {
                tc_data->dellocate(ptr, sp->size_);
            }
        }
    }
}


#endif